//url模块用于URL相关操作

const url = require('url');

//url.parse()解析URL
let obj = url.parse('http://fanyi.baidu.com:88/translate?aldtype=16047&query=track&keyfrom=baidu&smartresult=dict&lang=auto2zh#en/zh/track');
console.log(obj);

//url.parse()第二个参数指定是否解析query
obj = url.parse('http://fanyi.baidu.com:88/translate?aldtype=16047&query=track&keyfrom=baidu&smartresult=dict&lang=auto2zh#en/zh/track', true);
console.log(obj);

//url.parse()第三个参数指定是否忽略判断协议
obj = url.parse('//fanyi.baidu.com:88/translate?aldtype=16047&query=track&keyfrom=baidu&smartresult=dict&lang=auto2zh#en/zh/track', true, true);
console.log(obj);

//url.format()构建URL
let str = url.format(obj);
console.log(str);

//url.resolve()相对构建URL
let url2 = url.resolve(str, "kk1/kk2/");
console.log(url2);
let url3 = url.resolve(url2, "kk3/");
console.log(url3);